﻿using System;
using Microsoft.Win32;

namespace CMS.Web
{
	//
	//===============================================
		//Class dùng cho việc thao tác với Registry
	//===============================================
	public class Reg
	{
        /// <summary>
        /// 
        /// </summary>
		public  enum Hive 
		{
			HKEY_CLASSER_ROOT = RegistryHive.ClassesRoot,
			HKEY_CURRENT_USER = RegistryHive.CurrentUser,
			HKEY_LOCAL_MACHINE = RegistryHive.LocalMachine,
			HKEY_CURRENT_CONFIG = RegistryHive.CurrentConfig,
			HKEY_USERS = RegistryHive.Users
		};
		//---------------------------------------------------
			//Lấy một key đã tồn taị trong Registry
		//---------------------------------------------------
		private static RegistryKey Reg_key(Hive hive, string keypath, bool bWrite)
		{
			RegistryKey reg_key = null;			
			try
			{
				switch (hive)
				{
					case Hive.HKEY_CLASSER_ROOT:
						reg_key = Registry.ClassesRoot.OpenSubKey(keypath,bWrite);
						break;
					case Hive.HKEY_CURRENT_CONFIG:
						reg_key = Registry.CurrentConfig.OpenSubKey(keypath,bWrite);
						break;
					case Hive.HKEY_CURRENT_USER:
						reg_key = Registry.CurrentUser.OpenSubKey(keypath,bWrite);
						break;
					case Hive.HKEY_LOCAL_MACHINE:
						reg_key = Registry.LocalMachine.OpenSubKey(keypath,bWrite);
						break;
					case Hive.HKEY_USERS:
						reg_key = Registry.Users.OpenSubKey(keypath,bWrite);
						break;			
				}
				
			}
			catch
			{
				return null;
			}
			return reg_key;
		}
		//---------------------------------------------------
			//tạo một Key mới trong Registry
		//---------------------------------------------------
		private static RegistryKey pRegKey_new(Hive hive,string keypath,string strValue)
		{
			RegistryKey reg_key = null; 
			//string hivename ="";
			try
			{
				switch (hive)
				{
					case Hive.HKEY_CLASSER_ROOT:
						//hivename = "HKEY_CLASSER_ROOT";
						reg_key = Registry.ClassesRoot.CreateSubKey(keypath);
						break;
					case Hive.HKEY_CURRENT_CONFIG:
						//hivename = "HKEY_CURRENT_CONFIG";
						reg_key = Registry.CurrentConfig.CreateSubKey(keypath);
						break;
					case Hive.HKEY_CURRENT_USER:
						//hivename = "HKEY_CURRENT_USER";
						reg_key = Registry.CurrentUser.CreateSubKey(keypath);
						break;
					case Hive.HKEY_LOCAL_MACHINE:
						//hivename = "HKEY_LOCAL_MACHINE";
						reg_key = Registry.LocalMachine.CreateSubKey(keypath);
						break;
					case Hive.HKEY_USERS:
						//hivename = "HKEY_USERS";
						reg_key = Registry.Users.CreateSubKey(keypath);
						break;			
				}   				
				reg_key.SetValue(strValue,"0");
			}
			catch(Exception _err) 
			{
				//System.Windows.Forms.MessageBox.Show(_err.Message,"err");
				return null;}
			return reg_key;
		}
		public static string GetValue(string keypath,string keyname)
		{
			return GetValue(Hive.HKEY_LOCAL_MACHINE, keypath, keyname);
		}
		//---------------------------------------------------
			//Đọc giá trị từ Registry	
		//---------------------------------------------------
		public static string GetValue(Hive hive, string keypath,string keyname)
		{
			string ret = "";
			RegistryKey reg_key = Reg_key(hive, keypath, false);
			try
			{   							
				if(reg_key != null)
				{
					ret = ((string)reg_key.GetValue(keyname));
					reg_key.Close();
					if(ret == null)
						return "";
				}
			}
			catch(Exception _err)
			{
				//System.Windows.Forms.MessageBox.Show(_err.Message,"Thong bao");
				return null;
			}
			string tmp = Encrypt1.Encrypt(ret);
			if(tmp.IndexOf("\\") < 0)
			{
				if(Encrypt.DeEncryption(tmp, ref tmp)) 
					return tmp;
				else
					return "";
			}
			else
				return tmp;
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hive"></param>
        /// <param name="keypath"></param>
        /// <param name="keyname"></param>
        /// <returns></returns>
        public static string GetValues(Hive hive, string keypath, string keyname)
        {
            string ret = "";
            RegistryKey reg_key = Reg_key(hive, keypath, false);
            try
            {
                if (reg_key != null)
                {
                    ret = ((string)reg_key.GetValue(keyname));
                    reg_key.Close();
                    if (ret == null)
                        return "";
                }
            }
            catch (Exception _err)
            {
                //System.Windows.Forms.MessageBox.Show(_err.Message, "Thong bao");
                return "";
            }
            return ret;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keypath"></param>
        /// <param name="keyname"></param>
        /// <param name="strvalue"></param>
		public static void WriteValue(string keypath,string keyname, string strvalue)
		{
			  WriteValue(Hive.HKEY_LOCAL_MACHINE, keypath, keyname, strvalue);
		}
		//---------------------------------------------------
			//Ghi một giá trị vào Registry
		//---------------------------------------------------
		public static void WriteValue(Hive hive,string keypath,string keyname, string strvalue)
		{
			if(strvalue == null)
				strvalue = "";
			RegistryKey reg_key = Reg_key(hive, keypath, true);	
			try
			{  		
				if (reg_key == null)
					reg_key = pRegKey_new(hive, keypath, keyname);
				if(reg_key!=null)
				{ 					
					string tmp= "";
					//Mã hoá lần một
					if(strvalue.Equals(""))
						reg_key.SetValue(keyname, "");
					else
					if(strvalue.IndexOf("\\") < 0)
					{
						if(Encrypt.Encryption(strvalue, ref tmp))
						{
							//Mã hoá lần hai trước khi lưu
							reg_key.SetValue(keyname, Encrypt1.Encrypt(tmp));
						}
					}
					else
					{
						//Mã hoá lần hai trước khi lưu
						reg_key.SetValue(keyname, Encrypt1.Encrypt(strvalue));
					}
					reg_key.Close();	
				}
			}
			catch(Exception _err)
			{
				//System.Windows.Forms.MessageBox.Show(_err.Message,"Thong bao");				
			} 			
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hive"></param>
        /// <param name="keypath"></param>
        /// <param name="keyname"></param>
        /// <param name="strvalue"></param>
        public static void WriteValues(Hive hive, string keypath, string keyname, string strvalue)
        {
            if (strvalue == null)
                strvalue = "";
            RegistryKey reg_key = Reg_key(hive, keypath, true);
            try
            {
                if (reg_key == null)
                    reg_key = pRegKey_new(hive, keypath, keyname);
                if (reg_key != null)
                {
                    if (strvalue.Equals(""))
                        reg_key.SetValue(keyname, "");
                    else
                        reg_key.SetValue(keyname, strvalue);
                    reg_key.Close();
                }
            }
            catch (Exception _err)
            {
                //System.Windows.Forms.MessageBox.Show(_err.Message, "Thong bao");
            }
        }
		//---------------------------------------------------
			//Xoá một giá trị trị trong Registry
		//---------------------------------------------------
		public static void DeleteValue(Hive hive,string keypath,string keyname)
		{
			try
			{
				RegistryKey reg_key = Reg_key(hive,keypath,true);
				reg_key.DeleteValue(keyname);
			}
			catch{}
		}
	}
}
